package com.csoa.community.bean;

import java.util.ArrayList;
import java.util.List;

import com.csoa.community.utils.ConvertUtils;

public class SoapBody {
	private String method;
	private String action;
	private String desKey;

	public String getDesKey() {
		return desKey;
	}

	public void setDesKey(String desKey) {
		this.desKey = desKey;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public void setAction(String action) {
		this.action = action;
	}

	private List<SoapObj> params;

	public List<SoapObj> getParams() {
		return params;
	}

	public void setParams(List<SoapObj> params) {
		this.params = params;
	}
    public String getValueWithKey(int postion)
    {
    	return params.get(postion).value;
    }
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

		sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");

		sb.append("<soap:Body>");
		sb.append("<").append(method).append(" xmlns=\"http://tempuri.org/IArchives\">");
		for (SoapObj obj : params) {
			if (obj != null) {
				sb.append(obj);
			}
		}
		sb.append("</").append(method).append(">");
		sb.append("</soap:Body>");
		sb.append("</soap:Envelope>");

		return sb.toString();
	}

	/**
	 * 
	 * @param name
	 *            在xml中表示为tag
	 * @param value
	 *            在xml中表示为value 源
	 * @param key
	 *            将源以key的方式加密
	 */
	public void putValues(String name, String value, String key) {
		if (params == null) {
			params = new ArrayList<SoapObj>();
		}
		SoapObj obj = new SoapObj(name, value, key);
		params.add(obj);
	}

	private class SoapObj {
		String name;
		String value;
		String key;

		public SoapObj(String name, String value, String key) {
			this.name = name;
			this.value = value;
			this.key = key;
		}

//		@Override
//		public String toString() {
//			return "<" + name + ">" + ConvertUtils.encrpt(value, key) + "</" + name + ">";
//		}

	}

	public String getMethod() {
		return method;
	}
	public String getAction() {
		return action;
	}
}
